package userInfo

import (
	"github.com/gofiber/fiber/v2"
	"gorm.io/datatypes"
	"sg-backend/global"
	"sg-backend/model/user"
	"time"
)

type userInfo struct {
	UserId       uint           `json:"userId"`
	Username     string         `json:"username"`
	Name         string         `json:"name"`
	IsSuperuser  bool           `json:"isSuperuser"`
	NickName     string         `json:"nickName"`
	Avatar       string         `json:"avatar"`
	Mobile       string         `json:"mobile"`
	IsFirstLogin bool           `json:"isFirstLogin"`
	Title        string         `json:"title"`
	Email        string         `json:"email"`
	Gender       uint           `json:"gender"`
	CreateTime   time.Time      `json:"createTime"`
	FamilyType   datatypes.JSON `json:"familyType"`
	PaymentType  datatypes.JSON `json:"paymentType"`
	IncomeType   datatypes.JSON `json:"incomeType"`
}

func GetUserInfo(ctx *fiber.Ctx) error {

	// 获取用户信息
	userObj := user.User{Id: global.AuthUser.UserId}
	r := global.DB.First(&userObj)
	if r.Error != nil {
		return ctx.JSON(fiber.Map{
			"success": false,
			"message": "用户匹配失败！",
		})
	}

	// 开始处理
	var _userInfo = userInfo{
		UserId:       userObj.Id,
		Username:     userObj.Username,
		Name:         userObj.Username,
		IsSuperuser:  userObj.IsSuperuser,
		NickName:     userObj.Nickname,
		Avatar:       userObj.Avatar,
		Mobile:       userObj.Mobile,
		Email:        userObj.Email,
		Gender:       userObj.Gender,
		IsFirstLogin: userObj.IsFirstLogin,
		CreateTime:   userObj.CreatedTime,
	}

	// 返回数据
	return ctx.JSON(fiber.Map{
		"success": true,
		"data":    _userInfo,
	})
}
